package com.hbnu.service.impl;

import com.hbnu.dao.DormManagerDao;
import com.hbnu.dao.RecordDao;
import com.hbnu.dao.impl.DormManagerDaoImpl;
import com.hbnu.dao.impl.RecordDaoImpl;
import com.hbnu.pojo.DormBuild;
import com.hbnu.pojo.PageModel;
import com.hbnu.pojo.Record;
import com.hbnu.pojo.User;
import com.hbnu.service.RecordService;

import java.util.List;

public class RecordServiceImpl implements RecordService {

    RecordDao recordDao=new RecordDaoImpl();
    DormManagerDao dormManagerDao=new DormManagerDaoImpl();
    @Override
    public List<Record> findRecords(String startDate, String endDate, String dormBuildId, String searchType, String keyword, User userCurr, PageModel pageModel) {
        StringBuffer sql=new StringBuffer("select record.*,record.id recordId,record.disabled recordDisabled,user.*,build.name buildName from tb_record record" +
                " left join tb_user user on user.id=record.student_id" +
                " left join tb_dormbuild build on build.id=user.dormBuildId" +
                " where 1=1");

        if(keyword!=null&&!"".equals(keyword)&&"name".equals(searchType)){
            sql.append(" and user.name like '%"+ keyword.trim()+"'");
        }else if(keyword!=null&&!"".equals(keyword)&&"sex".equals(searchType)){
            sql.append(" and user.sex = '"+keyword.trim()+"'");
        }else if(keyword!=null&&!"".equals(keyword)&&"stuCode".equals(searchType)){
            sql.append(" and user.stu_code = '"+keyword.trim()+"'");
        }else if(keyword!=null&&!"".equals(keyword)&&"dormCode".equals(searchType)){
            sql.append(" and user.dorm_code = '"+ keyword.trim()+"'");
        }

        if(dormBuildId!=null&&!"".equals(dormBuildId)){
            sql.append(" and user.dormBuildId = '"+keyword.trim()+"'");
        }
        if(startDate!=null&&!"".equals(startDate)){
            sql.append(" and record.date >='"+startDate+"'");
        }
        if(endDate!=null&&!"".equals(endDate)){
            sql.append(" and record.date<='"+endDate+"'");
        }

        //获取当前登录的角色
        Integer roleId= userCurr.getRoleId();
        if(roleId!=null&&"1".equals(roleId)){
            List<DormBuild> dormBuilds=dormManagerDao.findByUserId(userCurr.getId());//获取当前管理员所管理楼栋

            sql.append(" and user.dormBuildId in (");
            for (int i = 0; i < dormBuilds.size(); i++) {
                sql.append(dormBuilds.get(i).getId()+",");
            }

            //删除最后一个
            sql.deleteCharAt(sql.length()-1);
            sql.append(")");
        }

        if(roleId!=null&&"2".equals(roleId)){
            sql.append(" and user.id="+userCurr.getId());//当前登录为学生
        }

        //缺勤日期递减查询
        sql.append(" order by record.date desc");

        //分页查询
        sql.append(" limit "+ pageModel.getStartNum()+","+pageModel.getPageSize());

        return recordDao.find(sql.toString());
    }

    @Override
    public Integer getTotalNum(String startDate, String endDate, String dormBuildId, String searchType, String keyword, User userCurr) {
        StringBuffer sql=new StringBuffer("select count(*) from tb_record record" +
                " left join tb_user user on user.id=record.student_id" +
                " where 1=1");

        if(keyword!=null&&!"".equals(keyword)&&"name".equals(searchType)){
            sql.append(" and user.name like '%"+ keyword.trim()+"'");
        }else if(keyword!=null&&!"".equals(keyword)&&"sex".equals(searchType)){
            sql.append(" and user.sex = '"+keyword.trim()+"'");
        }else if(keyword!=null&&!"".equals(keyword)&&"stuCode".equals(searchType)){
            sql.append(" and user.stu_code = '"+keyword.trim()+"'");
        }else if(keyword!=null&&!"".equals(keyword)&&"dormCode".equals(searchType)){
            sql.append(" and user.dorm_code = '"+ keyword.trim()+"'");
        }

        if(dormBuildId!=null&&!"".equals(dormBuildId)){
            sql.append(" and user.dormBuildId = '"+keyword.trim()+"'");
        }
        if(startDate!=null&&!"".equals(startDate)){
            sql.append(" and record.date >='"+startDate+"'");
        }
        if(endDate!=null&&!"".equals(endDate)){
            sql.append(" and record.date<='"+endDate+"'");
        }

        //获取当前登录的角色
        Integer roleId= userCurr.getRoleId();
        if(roleId!=null&&"1".equals(roleId)){
            List<DormBuild> dormBuilds=dormManagerDao.findByUserId(userCurr.getId());//获取当前管理员所管理楼栋
            sql.append(" and user.dormBuildId in (");
            for (int i = 0; i < dormBuilds.size(); i++) {
                sql.append(dormBuilds.get(i).getId()+",");
            }

            //删除最后一个
            sql.deleteCharAt(sql.length()-1);
            sql.append(")");
        }

        if(roleId!=null&&"2".equals(roleId)){
            sql.append(" and user.id="+userCurr.getId());//当前登录为学生
        }

        return recordDao.getTotalNum(sql.toString());
    }

    @Override
    public Record findById(int id) {
        return recordDao.findById(id);
    }

    @Override
    public void update(Record record) {
        recordDao.update(record);
    }

    @Override
    public void save(Record record) {
        recordDao.save(record);
    }
}
